package qsl.utils;

import java.io.*;
import java.net.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.Map.Entry;

import qsl.utils.Cookie;

import android.os.Handler;

public class Http {

	public static String GetString(String url) {
		return GetString(url, Charset.defaultCharset());
	}
	public static String GetString(String url, Cookie cookie) {
		return GetString(url, Charset.defaultCharset(), cookie);
	}

	public static String GetString(String url, Charset charset) {
		return GetString(url, null, charset);
	}
	public static String GetString(String url, Charset charset, Cookie cookie) {
		return GetString(url, null, charset, cookie);
	}

	public static String GetString(String url, Map<String, String> data) {
		return GetString(url, data, Charset.defaultCharset());
	}
	public static String GetString(String url, Map<String, String> data, Cookie cookie) {
		return GetString(url, data, Charset.defaultCharset(), cookie);
	}
	
	public static String GetString(String url, Map<String, String> data, Charset charset) {
		return GetString(url, data, charset, null);
	}

	public static String GetString(String url, Map<String, String> data, Charset charset, Cookie cookie) {
		StringBuffer sb = new StringBuffer();
		String line = null;
		BufferedReader buffer = null;

		try {
			InputStream stream = GetStream(new URL(url), data, cookie);
			InputStreamReader reader = new InputStreamReader(stream, charset);
			buffer = new BufferedReader(reader);
			while ((line = buffer.readLine()) != null)
				sb.append(line);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (buffer != null) buffer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	public static InputStream GetStream(URL url) {
		return GetStream(url, null, null);
	}

	public static InputStream GetStream(URL url, Map<String, String> data, Cookie cookie) {
		try {
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			if (cookie != null) con.setRequestProperty("cookie", cookie.toString());
			 
			if (data != null) {
				con.setDoOutput(true);
				con.setDoInput(true);
				con.setRequestMethod("POST");
				con.setUseCaches(false);
				con.setRequestProperty("content-type", "application/x-www-form-urlencoded");
				con.addRequestProperty("referer", "http://bbs.meizu.com/");
				con.connect();

				DataOutputStream out = new DataOutputStream(con.getOutputStream());
				String d = Map2String(data);
				out.writeBytes(d);
				out.flush();
			}
			InputStream stream = con.getInputStream();
			List<String> c = con.getHeaderFields().get("set-cookie");
			if (cookie != null) cookie.set(c);
			return stream;
		} catch (Exception e) {
			return null;
		}
	}

	private static String Map2String(Map<String, String> map) {
		StringBuffer sb = new StringBuffer();

		Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {
			Entry<String, String> entry = (Entry<String, String>) iterator.next();
			sb.append(entry.getKey() + "=" + entry.getValue());
			if (iterator.hasNext())
				sb.append("&");
		}
		return sb.toString();
	}

	

	public static void GetString(final String url, final Callback callback, int delay) {
		new Handler().postDelayed(new Runnable() {
	    	public void run() {
	    		callback.execute(GetString(url));
	    	}
		}, delay);
	}
	
	public static void GetString(final String url, final Charset charset, final Callback callback, int delay) {
		new Handler().postDelayed(new Runnable() {
	    	public void run() {
	    		callback.execute(GetString(url, charset));
	    	}
		}, delay);
	}
}




















